<?php
class Aoperations {
	const INTERLIMIT = 3;
	public static function main($funcode) {
		sview::verify(true, true);
		switch ($funcode) {
			case 1:
				self::__doreport();
				break;
			case 2:
				self::__dosndgift();
				break;
			case 3:
				self::__dobuyguard();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//举报信息5-2-1
	private static function __doreport() {
		$userid = acl::getinst('vedio')->user['id'];
		$ckey   = 'rt_limit_'.$userid;
		$snum   = (int)cache::getinst()->get($ckey);
		if (self::INTERLIMIT <= $snum) {
			sview::$view->assign_error(80063, lang::replace(80063, self::INTERLIMIT));
		}
		$data   = array('status'=>0, 'userid'=>$userid, 'ip'=>get_ip(), 'stime'=>config::$nowtime);
		$data['aimid']    = intval(sview::$ctrl['studioid']);
		$data['anchorid'] = intval(sview::$ctrl['anchorid']);
		$data['ttype']    = intval(sview::$ctrl['ttype']);
		$data['otype']    = intval(sview::$ctrl['otype']);
		$data['msg']      = trim(input::get_post('msg'));
		if (empty($data['msg'])) {
			$data['msg']= DDreport::$ttype[$data['ttype']];
		} else {
			$data['msg']= htmlspecialchars($data['msg']);
		}
		$attachs        = input::post('attachs');
		if (is_array($attachs)) {
			$data['attachs'] = implode(',', $attachs);
		}
		if ($snum < 1) {
			$expire = strtotime(date('Y-m-d', strtotime('+1 day'))) - config::$nowtime;
			cache::getinst()->set($ckey, 1, $expire);
		} else {
			cache::getinst()->increment($ckey, 1);
		}
		model::load('live/report')->new_one($data);
		sview::$view->display();
	}
	
	//礼物赠送5-2-2
	private static function __dosndgift() {
		$gid    = intval(sview::$ctrl['giftid']);
		$sid    = intval(sview::$ctrl['studioid']);
		$num    = intval(sview::$ctrl['nums']);
		$index  = isset(sview::$ctrl['index'])? intval(sview::$ctrl['index']):1;
		$userid = acl::getinst('vedio')->user['id'];
		if ($num < 1) {
			$num= 1;
		}
		//判断是否已经邮箱验证过了
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if (!$user || !$user['emailverify']) {
			sview::$view->assign_error(80068, lang::text(80068));
		}
		$studio = model::load('live/studio')->get_one($sid);
		$gift   = model::load('live/gift')->get_one($gid);
		if (!$studio || !$gift) {
			sview::$view->assign_error(80060, lang::text(80060));
		}
		if ($studio['userid'] == $userid) {
			sview::$view->assign_error(80107, lang::text(80107));
		}
		//判断送礼条件 -财富等级限制 --守护限制
		if ($gift['reglv'] > 0 && $user['lv'] < $gift['reglv']) {
			sview::$view->assign_error(80061, lang::text(80061));
		}
		if ($gift['isguard'] && !api_guard::get($userid, $sid)) {
			sview::$view->assign_error(80062, lang::text(80062));
		}
		//取当前的直播场次
		$eid    = api_studio::geteid($sid, $studio['userid']);
		//判断当前用户购买礼物的余额是否充足
		$coin   = $num * $gift['coinprice'];
		$bean   = $num * $gift['beanprice'];
		//开始记录扣款消费资料信息
		if(!api_exp::money($coin, $bean, $userid)) {
			sview::$view->assign_error(80030, lang::text(80030));
		}
		api_exp::incr($coin, $bean, 1, $gift['id'], $userid, $studio['userid'], $num);
		//返回剩余的金币资料信息
		$finace  = model::load('live/finance')->set_devision($userid)->get_one($userid);
		$dataset = array('coin'=>$finace['lastcoin'], 'gid'=>$gid, 'num'=>$num, 'bean'=>$finace['lastbean']);
		sview::$view->assign_by_ref('datasets', $dataset);
		sview::$view->display();
		//刷新数据到浏览器然后停止
		if (function_exists('fastcgi_finish_request')) {
			fastcgi_finish_request();
		}
		ignore_user_abort(true);
		//记录送礼流水信息记录
		ob_start();
		//礼物的赠送提醒信息
		$rds   = cache::getinst('redis')->instance();
		$mcoin = (int)api_studio::evtonestat($sid, $userid, $rds); 
		$msg   = json_encode(array('gift'=>$gid, 'num'=>$num, 'index'=>$index, 'coin'=>$mcoin+$coin, 'uin'=>$userid));
		api_im::notifystudio($sid, $msg, 0, 20015);
		//判断礼物是否达到水平
		api_mission::cons_mission($userid, $sid, $studio['evtid'], $mcoin+$coin);
		//处理礼物流水日志
		$fields = array('senderid'=>$userid, 'revicerid'=>$studio['userid'], 'studioid'=>$sid, 'eventid'=>$eid,
			'giftid'=>$gid, 'nums'=>$num, 'coinprice'=>$gift['coinprice'], 'beanprice'=>$gift['beanprice'],
			'totalcoin'=>$coin, 'totalbean'=>$bean, 'ip'=>get_ip(), 'stime'=>config::$nowtime
		);
		model::load('live/giftwater')->set_devision($sid)->new_one($fields);
		//统计贡献排行版数据资料信息
		api_studio::evtstat($sid, $userid, $coin, $num, $rds);
		//经验变化判断升级等 并发送广播通知
		api_lv::notify($userid, $studio['userid'], $sid);
		ob_end_clean();
	}
	
	//守护购买5-2-3
	private static function __dobuyguard() {
		$gid    = 1; //intval(sview::$ctrl['guardid']);
		$sid    = intval(sview::$ctrl['studioid']);
		$nums   = intval(sview::$ctrl['nums']);
		$userid = acl::getinst('vedio')->user['id'];
		if ($nums < 1 || $nums > 12) {
			$nums = 1;
		}
		//判断是否已经邮箱验证过了
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		if (!$user || !$user['emailverify']) {
			sview::$view->assign_error(80068, lang::text(80068));
		}
		$guard  = model::load('live/guard')->get_one($gid);
		$studio = model::load('live/studio')->get_one($sid);
		if (empty($guard) || empty($studio)) {
			sview::$view->assign_error(80011, lang::text(80011));
		}
		if ($studio['userid'] == $userid) {
			sview::$view->assign_error(80107, lang::text(80107));
		}
		//年守护，时间要相乘
		$money   = $nums * $guard['coinprice'];
		$bean    = $nums * $guard['beanprice'];
		$airtime = (($gid == api_guard::YRefType)? 365:30) * $nums * 86400;
		//不是续费的购买还是允许的，不必验证人数的是否超过限制
		if (!api_guard::get($userid, $sid) && api_guard::total($sid) >= api_guard::YMAXSIZE) {
			sview::$view->assign_error(80057, lang::text(80057));
		}
		//更新财务信息表
		if(!api_exp::money($money, $bean, $userid)) {
			sview::$view->assign_error(80030, lang::text(80030));
		}
		//更新财富和主播经验值经验值 还有购买流水
		api_exp::incr($money, $bean, 4, $gid, $userid, $studio['userid'], $nums);
		//更新道具购买记录表
		$res = api_mypros::buy($userid, 2, $gid, $airtime, $nums, $sid);
		//将会员的经验值和主播经验值广播下去
		api_guard::set($userid, $sid);
		//判断看看是否赠送座驾道具,如果需要道具表新增一条记录
		$setting = json_decode($guard['settings'], true);
		if ($setting && isset($setting['motorid']) && $setting['motorid'] > 0) {
			$motor = api_mypros::buy($userid, 1, $setting['motorid'], $airtime, 1, $sid);
			if ($user['motorid'] < 1 && $motor) {//未穿戴座驾的时候自动穿戴上
				model::load('live/userinfo')->set_devision($userid)->save($userid, array('motorid'=>$motor['id']));
			}
		}
		//给主播发送站内信提醒
		$lang   = ($gid == api_guard::YRefType)? 80066:80067;
		$msg = lang::replace(80058, $user['nickname'], lang::text($lang));
		api_sitemsg::send(lang::text(80059), $msg, $studio['userid'], 2, $userid);
		//客户端给主播发送push提醒
		$nickname = htmlspecialchars_decode($user['nickname']);
		api_sitemsg::push($studio['userid'], lang::replace(80132, $nickname), lang::replace(80132, $nickname), 103, 0, $userid);
		//重置当前用户的权限资料 判断升级，然后广播出去
		$access = api_access::reset($userid, $sid);//冗余字段nums不能删除
		$rds   = cache::getinst('redis')->instance();
		$mcoin = (int)api_studio::evtonestat($sid, $userid, $rds);
		$data = array('userid'=>$userid, 'month'=>$nums, 'coin'=>$mcoin+$money,
				'expire'=>$res['expire'], 'nums'=>$nums, 'guard'=>$gid, 'access'=>$access);
		api_im::notifystudio($sid, json_encode($data), 0, 20018);
		//判断礼物是否达到水平
		api_mission::cons_mission($userid, $sid, $studio['evtid'], $mcoin+$money);
		//判断升级条件的通知
		api_lv::notify($userid, $studio['userid'], $sid);
		//完成购买守护的逻辑
		api_mission::vip_mission($userid, $sid);
		//统计贡献排行版数据资料信息
		api_studio::evtstat($sid, $userid, $money, 0, $rds);
		//返回剩余的金币资料信息
		$finace = model::load('live/finance')->set_devision($userid)->get_one($userid);
		$data   = array('coin'=>$finace['lastcoin'], 'bean'=>$finace['lastbean'], 'expire'=>$res['expire']);
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
}